﻿#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <numeric>


using namespace std;
struct TreeNode {
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
	
};
class Node {
public:
	int val;
	Node* left;
	Node* right;
	Node* next;

	Node() : val(0), left(NULL), right(NULL), next(NULL) {}

	Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}

	Node(int _val, Node* _left, Node* _right, Node* _next)
		: val(_val), left(_left), right(_right), next(_next) {}
};
struct ListNode {
	int val;
	ListNode *next;
	ListNode() : val(0), next(nullptr) {}
	ListNode(int x) : val(x), next(nullptr) {}
	ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution {
public:
	int videoStitching(vector<vector<int>>& clips, int T) {
		sort(clips.begin(), clips.end());
		int ans = 0;
		int i = 0;
		int start = 0;
		int end = -1;
		int tmp = 0;
		while (end<T) {
			if (i >= clips.size() && clips[i][0] > start)
				return -1;
			for (i = tmp; i < clips.size(); i++) {
				if (clips[i][0] <= start && clips[i][1] > end) {
					tmp = i;
					end = clips[i][1];
				}
				else if (clips[i][0] > start) {
					start = end;
					break;
				}
			}
			ans++;
		}
		return ans > 0 ? ans : -1;
	}
};


int main()
{
	Solution sol;
	vector<vector<int>> test = { {0, 1}, {6, 8}, {0, 2}, {5, 6}, {0, 4}, {0, 3}, {6, 7}, {1, 3}, {4, 7}, {1, 4}, {2, 5}, {2, 6}, {3, 4}, {4, 5}, {5, 7}, {6, 9} };
	vector<vector<int>> test2 = {{0, 2}, {4, 6}, {8, 10}, {1, 9}, {1, 5}, {5, 9}};
	ListNode d(4);
	ListNode c(3, &d);
	ListNode b(2, &c);
	ListNode a(1, &b);
	vector<int> t = {1,0,2 };
	sol.videoStitching(test2,10);
	cout <<endl;
}
